package offer.diff;

/**
 * @author DengYuan2
 * @create 2021-04-11 20:00
 */
public class n_13_2 {
    public static void main(String[] args) {
        n_13_2 n = new n_13_2();
        int res = n.movingCount(5, 30, 40);
        System.out.println(res);
    }

    public int count=0;
    public int rols=0;
    public int cols=0;

    public int movingCount(int threshold, int rows, int cols) {
        //情况 0-没走过 1-走过 2-不能走
        int[][] condition=new int[rows][cols];
        this.rols=rows;
        this.cols=cols;
        helper(threshold,0,0,condition);
        return count;
    }

    public void helper(int threshold,int x,int y,int[][] condition){
        if (x<0||x>=rols||y<0||y>=cols||condition[x][y]==1){
            return;
        }
        if (getSum(x)+getSum(y)>threshold){
            condition[x][y]=2;
            return;
        }
        if (condition[x][y]==2){
            return;
        }
        count++;
        condition[x][y]=1;
        //左
        helper(threshold,x,y-1,condition);
        //右
        helper(threshold,x,y+1,condition);
        //上
        helper(threshold,x-1,y,condition);
        //下
        helper(threshold,x+1,y,condition);

    }

    public int getSum(int x){
        int sum=0;
        while (x!=0){
            int y=x%10;
            sum+=y;
            x/=10;

        }
        return sum;
    }
}
